<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.qiqianyi.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\company\controller;

use app\admin\model\RouteModel;
use app\company\model\CompanyCategoryModel;
use cmf\controller\AdminBaseController;
use app\company\model\MemberModel;
use think\Db;
use app\company\service\CategoryService;
use app\company\service\CompanyService;
use app\common\model\DistrictModel;
use app\company\validate\AdminUserValidate;
use app\user\model\UserModel;
use app\company\model\CompanyModel;


class AdminUserController extends AdminBaseController
{


    public function addBase(){

        $memberStatusList = CategoryService::queryMemberStatusList();
        $this->assign('member_status', $memberStatusList);

        return $this->fetch();
    }


    public function addBasePost()
    {


            if ($this->request->isPost()) {
                $data   = $this->request->param();
                $post   = $data['post'];

                $post["user_pass"] = cmf_password('111111');
                $post["user_type"] = "3";
                $post["create_time"] =  time();
                $post["birthday"] =    strtotime($post["birthday"]);


                $result = $this->validate($post, 'AdminUser');
                    if ($result !== true) {
                        $this->error($result);
                    } else {
                        $result             = DB::name('user')->insertGetId($post);



                        if ($result !== false) {

                            $this->success("添加成功！", url("/user/admin_index/index"));
                        } else {
                            $this->error("添加失败！");
                        }
                    }
                }

    }

    public function editBase(){


        $memberStatusList = CategoryService::queryMemberStatusList();
        $this->assign('member_status', $memberStatusList);



        $id = $this->request->param('id', 0, 'intval');

        $userModel = new UserModel();
        $post            = $userModel->where('id', $id)->find();


        $this->assign('post', $post);
        return $this->fetch();
    }


    public function editBasePost()
    {


        if ($this->request->isPost()) {
            $data   = $this->request->param();
            $post   = $data['post'];

            $post["birthday"] =    strtotime($post["birthday"]);

            //$post["user_pass"] = cmf_password('111111');
           // $post["user_type"] = "3";

            $result = $this->validate($post, 'AdminUser');
            if ($result !== true) {
                $this->error($result);
            } else {
                $result = DB::name('user')->update($post);



                if ($result !== false) {

                    $this->success("修改成功！", url("/user/admin_index/index"));
                } else {
                    $this->error("修改失败！");
                }
            }
        }

    }

    /**
     * 打开添加页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editMore(){

        $memberCategoryList= CategoryService::queryMemberCategoryList();
        $this->assign('member_category', $memberCategoryList);

        $breedStatusList = CategoryService::queryBreedStatusList();
        $this->assign('breed_status', $breedStatusList);

        $relationCategoryList = CategoryService::queryRelationCategoryList();
        $this->assign('relation_category', $relationCategoryList);

        $memberStatusList = CategoryService::queryMemberStatusList();
        $this->assign('member_status', $memberStatusList);


        $memberGroupList = CategoryService::queryMemberGroupList();
        $this->assign('member_group', $memberGroupList);



        $companyModel =new CompanyModel ();


        $where = [
            'delete_time'    => 0
        ];

        $order    = "list_order";

        $companyList = $companyModel->where($where)->order($order)->select();

        $this->assign('company_list', $companyList);





        $id = $this->request->param('id', 0, 'intval');

        $memberModel = new MemberModel();
        $post            = $memberModel->where('id', $id)->find();

        $this->assign('id', $id);
        $this->assign('post', $post);

        $defaultDistrict = $post['district_id'];
        $districts = $this->getDistrict($defaultDistrict);
        $this->assign("districts", $districts);
        $this->assign("default_district", $defaultDistrict);

        return $this->fetch();
    }



    public function editMorePost()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->param();



            $post   = $data['post'];

            //dump($data);

            if (!empty($data['file_names']) && !empty($data['file_urls'])) {
                $data['post']['more']['files'] = [];
                foreach ($data['file_urls'] as $key => $url) {
                    $fileUrl = cmf_asset_relative_url($url);
                    array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
                }
            }

            //dump($data);

            $result = $this->validate($data['post'], 'Member');
            if ($result !== true) {
                $this->error($result);
            }

            $memberModel = new MemberModel();
            $memberModel->addMember($data['post']);



            $this->success('保存成功!');

        }

    }

    public function getDistrict($id)
    {
        $districts = array(
            "name" => "",
            "options" => array(
                array("id" => 0, "options" => ""),
                array("id" => 0, "options" => ""),
                array("id" => 0, "options" => ""),
                array("id" => 0, "options" => "")
            )
        );
        $districtModel = new DistrictModel();
        $district = $districtModel->find($id);
        if (empty($district)) {
            $districts['options'][0]['options'] = $districtModel->where(array("parentid" => 0))->order("listorder")->select();;
        } else {
            if ($district['level'] < 4) {
                $districts['options'][$district['level']]["options"] = $districtModel->where(array("parentid" => $district['id']))->order("listorder")->select();
            }
            while ($district['level'] >= 1) {
                $districts['options'][$district['level'] - 1]["id"] = $district['id'];
                $districts['options'][$district['level'] - 1]["options"] = $districtModel->where(array("parentid" => $district['parentid']))->order("listorder")->select();
                $districts['name'] = $district['name'] . $districts['name'];
                $district = $districtModel->find($district['parentid']);
            }
        }
        return $districts;
    }


    public function district($parentid = 0, $type = "array")
    {
        $districtModel = new DistrictModel();
        $district = $districtModel->where(array("parentid" => $parentid))->order("listorder")->select();
        if ($type == "array") {
            return $district;
        } else {
            $this->result($district);
        }
    }

}
